package cn.yeziji.forum.converter;

import cn.yeziji.forum.common.websocket.Chatroom;
import cn.yeziji.forum.entity.ForumChatroomEntity;
import cn.yeziji.forum.vo.chatroom.RecordsVO;
import cn.yeziji.forum.vo.chatroom.UserChatMsgVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Set;

/**
 * 聊天室转换器
 *
 * @author gzkemays
 * @since 2022/3/27 13:46
 */
@Mapper
public interface ChatroomConverter {
  ChatroomConverter INSTANCE = Mappers.getMapper(ChatroomConverter.class);

  /**
   * websocket msg 转换为 entity 对象
   *
   * @param chatRoom 指定 websocket msg
   * @return entity
   */
  @Mapping(target = "context", source = "msg")
  ForumChatroomEntity wbMsgConvertToEntity(Chatroom chatRoom);

  /**
   * entity 转化为 websocket msg 对象
   *
   * @param entity entity
   * @return websocket msg
   */
  @Mapping(target = "msg", source = "context")
  Chatroom entityConvertToChatroom(ForumChatroomEntity entity);

  /**
   * entity转化为聊天记录对象
   *
   * @param entity entity
   * @return 聊天记录对象
   */
  RecordsVO entityConvertToRecordsVo(ForumChatroomEntity entity);

  /**
   * {@link #entityConvertToRecordsVo(ForumChatroomEntity)} list
   *
   * @param entities entity list
   * @return list records
   */
  List<RecordsVO> entitiesConvertToRecordsVos(List<ForumChatroomEntity> entities);
  /**
   * websocket msg 转换为 entity 列表
   *
   * @param chatrooms 指定 websocket msg列表
   * @return entities
   */
  List<ForumChatroomEntity> wbMsgConvertToEntities(List<Chatroom> chatrooms);
  /**
   * websocket msg 转换为 entity 列表
   *
   * @param chatrooms 指定 websocket msg列表
   * @return entities
   */
  Set<ForumChatroomEntity> wbMsgConvertToEntities(Set<Chatroom> chatrooms);

  /**
   * entity 转换为聊天信息对象
   *
   * @param entity entity
   * @return 聊天信息
   */
  @Mapping(target = "toView", expression = "java(entity.getUpdateTime() == null)")
  UserChatMsgVO entityConvertToChatMsgVo(ForumChatroomEntity entity);

  /**
   * {@link #entityConvertToChatMsgVo(ForumChatroomEntity)}} to list
   *
   * @param entities entity list
   * @return vo list
   */
  List<UserChatMsgVO> entitiesConvertToChatMsgVos(List<ForumChatroomEntity> entities);
}
